package com.team.arms.tijian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.arms.basic_info.domain.FamilyReigistInfo;
import com.team.arms.basic_info.mapper.FamilyReigistInfoMapper;
import com.team.arms.bingyi.domain.BingyiRegistInfo;
import com.team.arms.bingyi.mapper.BingyiRegistInfoMapper;
import com.team.arms.bingyiInfo.entity.BingyiInfo;
import com.team.arms.bingyiInfo.mapper.BingyiInfoMapper;
import com.team.arms.common.Result;
import com.team.arms.tijian.entity.vo.RecommendVo;
import com.team.arms.tijian.mapper.RecommendedMapper;
import com.team.arms.tijian.mapper.SysHealthExaminationMapper;
import com.team.arms.tijian.mapper.SysHealthFailedMapper;
import com.team.arms.tijian.service.IRecommendedService;
import com.team.arms.utils.ExcelUtil;
import entity.Recommended;
import entity.SysHealthExamination;
import entity.SysHealthFailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 主检推荐复检表 服务实现类
 * </p>
 *
 * @author Huazige
 * @since 2022-06-02
 */
@Service
public class RecommendedServiceImpl extends ServiceImpl<RecommendedMapper, Recommended> implements IRecommendedService {

    @Autowired
    RecommendedMapper recommendedMapper;

    @Autowired
    BingyiInfoMapper bingyiInfoMapper;
    @Autowired
    FamilyReigistInfoMapper familyReigistInfoMapper;
    @Autowired
    BingyiRegistInfoMapper bingyiRegistInfoMapper;
    @Autowired
    SysHealthFailedMapper sysHealthFailedMapper;
    @Autowired
    SysHealthExaminationMapper sysHealthExaminationMapper;


    @Override
    public Result importExcel(MultipartFile file) {


        String originalFilename = file.getOriginalFilename();
        String pattern = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);;
        List<List<String>> list = new ArrayList<>();



        if(file!=null){
            if (!ExcelUtil.isExcel(file)) {
                return Result.error("导入失败，该文件不是excel文件");
            }
            list = ExcelUtil.readExcelContents(file, pattern);
            if (list.isEmpty()) {
                return Result.error("表格为空");
            }
            System.out.println(list.size());
            for (int i = 0; i < list.size(); i++) {
                  //封装成vo的list对象，然后get在
//                String dateStr = listContent.get(i).get(5);
//                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                LocalDateTime parsedDate = LocalDateTime.parse(dateStr, formatter);
//                System.out.println(parsedDate);
                String bingyiInfoId = recommendedMapper.getbingyiInfoIdmax();
                String familyId = recommendedMapper.getfamilyIdmax();
                RecommendVo recommendVo = new RecommendVo();


                BingyiInfo bingyiInfo = new BingyiInfo();

                bingyiInfo.setName(list.get(i).get(1));
                bingyiInfo.setSex(list.get(i).get(2));
                bingyiInfo.setIdCard(list.get(i).get(3));
                bingyiInfo.setPhone(list.get(i).get(4));
                bingyiInfo.setBingyiInfoId(bingyiInfoId);
               bingyiInfoMapper.insert(bingyiInfo);
//                recommendedMapper.inser(list.get(i).get(2),list.get(i).get(3),list.get(i).get(4),list.get(i).get(5),bingyiInfoId);
               /* QueryWrapper<BingyiInfo> queryWrapperPolitical=new QueryWrapper<>();
                queryWrapperPolitical.eq("id_card",list.get(i).get(4));
                BingyiInfo bingyiInfo = bingyiInfoMapper.selectOne(queryWrapperPolitical);
                String bingyiInfoId = bingyiInfo.getBingyiInfoId();*/

                //获取家庭编号表
//                bingyiInfo.setBirthday(parsedDate);
                FamilyReigistInfo familyReigistInfo = new FamilyReigistInfo();
                familyReigistInfo.setFamilyDetail(list.get(i).get(5));
                familyReigistInfo.setFamilyId(familyId);
//                recommendedMapper.inserfamilyinfo(list.get(i).get(6),familyId);
                familyReigistInfoMapper.insert(familyReigistInfo);
               /* QueryWrapper<FamilyReigistInfo> queryWrapperPolitical2=new QueryWrapper<>();
                queryWrapperPolitical.eq("family_detail",list.get(i).get(6));
                FamilyReigistInfo familyReigistInfo = familyReigistInfoMapper.selectOne(queryWrapperPolitical2);
                String familyId = familyReigistInfo.getFamilyId();*/
                BingyiRegistInfo bingyiRegistInfo = new BingyiRegistInfo();
//                bingyiRegistInfo.setYear();
                bingyiRegistInfo.setYear(list.get(i).get(6));
                bingyiRegistInfo.setBingyiInfoId(bingyiInfoId);
                bingyiRegistInfo.setFamilyId(familyId);
                bingyiRegistInfoMapper.insert(bingyiRegistInfo);
                //体检日期暂时不设置

//                recommendedMapper.inserbingyiinfo(list.get(i).get(7),bingyiInfoId,familyId);
/*                LocalDateTime ldt =null;
//                SimpleDateFormat dataformatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
                Date date =null;

                if (!list.get(i).get(7).equals(null)||!list.get(i).get(7).equals(' ')){
//                    String s=    list.get(i).get(7);
//                    System.out.println(list.get(i).get(7));
//                    String[] split = s.split("-");
//                    String time =split[2]+"-"+split[1]+"-"+split[1];
                    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");

                     ldt = LocalDateTime.parse(list.get(i).get(7),dateFormat);
//                    try {
//                        date =dataformatter.parse(list.get(i).get(7));
//                    } catch (ParseException e) {
//                        e.printStackTrace();
//                    }
                }
                    recommendVo.setExamineTime(ldt);

                sysHealthFailed.setExamineTime(ldt);*/
                SysHealthFailed sysHealthFailed = new SysHealthFailed();

                sysHealthFailed.setBingyiInfoId(bingyiInfoId);
                sysHealthFailedMapper.insert(sysHealthFailed);
//                recommendedMapper.inserhealthfailed(ldt,bingyiInfoId);

                SysHealthExamination sysHealthExamination = new SysHealthExamination();
                sysHealthExamination.setExamineId(list.get(i).get(0));
                sysHealthExamination.setBingyiInfoId(bingyiInfoId);

                sysHealthExamination.setHealthExaminStats(list.get(i).get(8));
                sysHealthExamination.setBingyiInfoId(bingyiInfoId);
                sysHealthExaminationMapper.insert(sysHealthExamination);
//                recommendedMapper.inserhealthexamination(list.get(i).get(1),list.get(i).get(9),bingyiInfoId);

                //怎么知道对方excel表导入的顺序的如果他的顺序不对应呢？,还要根据对应的标题选择导入对应
                //相同的数据的判重问题。
                //我们是自定义vo得分别导入不同的数据库表中...
                //基础数据是不会变，所以不需要插入.
                //如何保证插入的时候保证各个编号一致性
//                recommendedMapper.
            }

            return Result.success(list);
        }
        return Result.error("未选择文件");
    }
}
